//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class CandidateRepository : BaseRepository, ICandidateRepository {
        public CandidateRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Candidate candidate) {
            this.GetDbSet<Candidate>().Add(candidate);
            this.UnitOfWork.SaveChanges();
        }

        public Candidate GetCandidate(Guid candidateId) {
            return this.GetDbSet<Candidate>()
                .Where(c => c.Id == candidateId)
                .Single();
        }

        public void Update(Candidate updatedCandidate) {
            this.GetDbSet<Candidate>().Attach(updatedCandidate);
            this.SetEntityState(updatedCandidate, updatedCandidate.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid candidateId) {
            Candidate CandidateToDelete = this.GetCandidate(candidateId);
            this.GetDbSet<Candidate>().Remove(CandidateToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Candidate> Find()
        {
            return this.GetDbSet<Candidate>();
        }

        public IQueryable<Candidate> GetAll()
        {
            return this.GetDbSet<Candidate>();
        }
    }
}
